# -*- coding:utf-8 -*-
# @Time : 2022/3/24 上午11:23 
# @Author : zdy
# @File : GraphBase.py
# desc:
from pyqtgraph import PlotWidget
import pyqtgraph as pg
from importQT import *

class GraphBase():
    def __init__(self, parent=None, title=''):
        # 进行全局设置：禁止拖动、前景色为黑色、背景色为白色；
        pg.setConfigOptions(leftButtonPan=False)
        pg.setConfigOption('background', 'w')
        pg.setConfigOption('foreground', 'k')
        pg.PlotItem().setMenuEnabled(False)
        pg.setConfigOptions(antialias= True)
        self.graphwiget = PlotWidget(parent, title=title)
        # self.base = [self.graphwiget.plot(pen=pg.mkPen(color=(128, 0, 0), width=2)),
        #              self.graphwiget.plot(pen=pg.mkPen(color=(30, 144, 255), width=2)),
        #              self.graphwiget.plot(pen=pg.mkPen(color=(255, 0, 255), width=2)),
        #              self.graphwiget.plot(pen=pg.mkPen(color=(255, 0, 0), width=2))]
        self.textData = pg.TextItem()
        self.textData.setVisible(False)
        self.graphwiget.addItem(self.textData, ignoreBounds=True)
        self.graphwiget.setMenuEnabled(False)
        self.graphwiget.setContextMenuPolicy(Qt.ActionsContextMenu)
        action0 = QAction("自动缩放", self.graphwiget)
        action0.triggered.connect(self.graph_zoom)
        self.graphwiget.addAction(action0)
        action1 = QAction("网格", self.graphwiget)
        action1.setCheckable(True)
        action1.triggered.connect(self.graph_grid)
        self.graphwiget.addAction(action1)
        action3 = QAction("显示数值", self.graphwiget)
        action3.setCheckable(True)
        action3.triggered.connect(self.graph_showValue)
        self.graphwiget.addAction(action3)

    def graph_grid(self):
        """pyqtgraph 网格设置"""
        if self.graphwiget.getPlotItem().ctrl.xGridCheck.checkState() == Qt.Checked:
            self.graphwiget.showGrid(False, False)
        else:
            self.graphwiget.showGrid(True, True)

    def graph_zoom(self):
        """pyqtgraph 自动缩放设置"""
        self.graphwiget.getPlotItem().vb.autoRange()
        self.graphwiget.getPlotItem().vb.enableAutoRange()  # 注意：autoRange()对于新增数据不适用
        pass

    def graph_showValue(self, b):
        if b:
            self.graphwiget.scene().sigMouseMoved.connect(self.graph_mousemoved)
            self.textData.setVisible(True)
            # self.vLine.setPen('b')
            # self.hLine.setPen('b')
        else:
            self.graphwiget.scene().sigMouseMoved.disconnect(self.graph_mousemoved)
            self.textData.setVisible(False)
            # self.vLine.setPen('w')
            # self.hLine.setPen('w')

    def graph_mousemoved(self, pos):
        """参数pos是像素坐标，需要转换为刻度坐标"""
        act_pos = self.graphwiget.getPlotItem().vb.mapSceneToView(pos)
        x = act_pos.x()
        y = act_pos.y()
        x_str = "(%0.3f" %x
        y_str = "%0.3f)" %y
        html_str = '<p style = "color:black;font-size:14px;font-weight:bold;">&nbsp;'+ x_str +'&nbsp;'+y_str+'</p>'
        self.textData.setHtml(html_str)
        self.textData.setPos(x, y)
        # self.vLine.setPos(x)
        # self.hLine.setPos(y)
        pass